// order//order/orderDetailCoupon/index.js
const app = getApp();
const utils = require('../../../utils/util.js');
import getUserCard from "../../../utils/getUserCard.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo:{},
    proInfo: {},
    codeInfo: {},
    expireTime: '',
    broprice: 0,
    CouponTypeId: 0,
    cardInfo: ''
  },

  getOrder: function(uid, oid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Order/GetOrder?uid=' + uid + '&oid=' + oid,
      success: function(res){
        if(res.data.Data){
          let orderinfo = res.data.Data.OrderInfo;
          let proInfo = res.data.Data.OrderProductList[0].Info;
          if(proInfo.ShowImg.indexOf('ps') != -1){
            proInfo.ShowImg = utils.formatAvaterByProHead(proInfo.ShowImg);
          }else{
            proInfo.ShowImg = utils.formatAvater(proInfo.ShowImg,'https://static.711688.net.cn/upload/store/'+ app.globalData.storeId +'/product/editor/');
          }
          that.getInfo(orderinfo.CreateUid);
          that.setData({
            orderInfo: orderinfo,
            proInfo: proInfo
          })
        }
      }
    })
  },
  toDetail: function(){
    wx.navigateTo({
      url: '/pages/productDetails/index?pid=' + this.data.proInfo.Pid + '&seltoid=' + this.data.proInfo.CreateUid,
    })
  },
  getInfo: function(createUid){
    let that = this;
    getUserCard(createUid, function (cardid) {
      wx.request({
        url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardid,
        success: function(res){
          if(res.data.Code == 0){
            let cardInfo = {};
            let address = res.data.Data.ProvinceName +  res.data.Data.CityName +  res.data.Data.AreaName + res.data.Data.Address;
            cardInfo.company = res.data.Data.Company;
            cardInfo.mobile = res.data.Data.Mobile;
            cardInfo.phone = res.data.Data.Phone;
            cardInfo.address = address;
            cardInfo.latitude = res.data.Data.Latitude;
            cardInfo.longitude = res.data.Data.Longitude;
            
            that.setData({
              cardInfo: cardInfo
            })
          }
        }
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrder(options.uid, options.oid);
    this.getCode(options.oid);
    if(options.expiretime){
      this.setData({
        expireTime: options.expiretime
      })
    }
    if(options.broprice){
      this.setData({
        broprice: options.broprice
      })
    }
    if(options.coupontypeid){
      this.setData({
        CouponTypeId: options.coupontypeid
      })
    }
  },

  getCode: function(oid){
    let that = this;
    wx.request({
      url: app.globalData.host+ '/api/order/GetOrderCheck?oid=' + oid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            let orderInfo = res.data.Data;
            orderInfo.codesnList = [];
            let codelist = orderInfo.CodeSN.split(",");
            let lengthIndex = codelist.length;
            for(let m = lengthIndex-1; m >= 0; m--){
              orderInfo.codesnList.push(codelist[m]);
            }
            if(orderInfo.codesnList.length>0){
              let list = orderInfo.codesnList;
              for(let i = 0; i < list.length; i++){
                list[i] = list[i].replace(/\s/g,'').replace(/(.{4})/g,"$1 ");
              }
              orderInfo.canUse = list.length - orderInfo.CheckCount;
            }
            that.setData({
              codeInfo: orderInfo
            })
          }
        }
      }
    })
  },

  // 再来一单
  buyAgainCoupon: function(e){
    let that = this;
    let pid = e.currentTarget.dataset.pid;
    let buycount = e.currentTarget.dataset.buycount;
    let createuid = e.currentTarget.dataset.createuid;
    that.addCouponCart(pid, buycount, createuid);
  },

   // 优惠券抢购
   addCouponCart: function(pid, buycount, createuid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Store/Product?pid=' + pid,
      success: function(res) {
        if (res.data.Code == 0) {
          var p = res.data.Data;
          if (p.ProductInfo.State == 1) {
            wx.showModal({
              title: '提示',
              content: '商品已下架',
              showCancel: false
            })
            return;
          }
          if (p.StockNumber < buycount) {
            wx.showModal({
              title: '提示',
              content: '商品库存不足',
              showCancel: false
            })
            return;
          }
          that.clearCart(createuid,function(flag) {
            if (flag) { //清空购物车完成
              wx.request({
                url: app.globalData.host + '/api/cart/AddProduct',
                data: {
                  uid: app.globalData.userInfo.Uid,
                  pid: pid,
                  buyCount: buycount,
                  optionIds: '',
                  createUid: createuid
                },
                success: function(res) {
                  if (res.data.Code == 0) {
                    // 跳转优惠券购物车页面
                    wx.navigateTo({
                      url: '/order/order/cart/index?isCoupon=1&stockNumber=' + p.StockNumber + '&seltoid=' + createuid,
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: res.data.Message,
                      showCancel: false
                    })
                  }
                }
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      },
    })
  },
  // 清空购物车
  clearCart: function(createuid,cb) {
    wx.request({
      url: app.globalData.host + '/api/Cart/Clear?uid=' + app.globalData.userInfo.Uid + '&createUid=' + createuid,
      success: function(res) {
        cb(res.data.Data);
      }
    })
  },

  tuikuan: function(e) {
    let oid = e.currentTarget.dataset.oid;
    let money = e.currentTarget.dataset.money;
    wx.navigateTo({
      url: '/order/order/tuikuan/index?oid=' + oid + '&money=' + money,
    })
  },
    //拨打电话
    makeCallPhone: function (e) {
      let mobile = e.currentTarget.dataset.mobile;
      let phone = e.currentTarget.dataset.phone;
      if (phone) {
        wx.makePhoneCall({
          phoneNumber: phone
        })
        utils.aiCardActionRecord(16);
      }
      if (mobile) {
        wx.makePhoneCall({
          phoneNumber: mobile
        })
        utils.aiCardActionRecord(9);
      }
    },
    
    openMapLocation: function () {
      var that = this;
      var cardInfo = that.data.cardInfo;
      wx.showModal({
        title: '提示',
        content: '是否打开地图导航',
        success: function (res) {
          if (res.confirm) {
            wx.openLocation({
              latitude: cardInfo.latitude,
              longitude: cardInfo.longitude,
              scale: 28,
              name: "点击右边打开地图导航",
              address: cardInfo.address
            })
          }
        }
      })
    },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})